const Database = require('./database/db');
const Vocabulary = require('./database/models/vocabulary');

async function checkWords() {
  const db = new Database();
  try {
    db.init('e:/codes/English-pi/data/ielts_vocabulary.db');
    console.log('Connected to database');

    const vocabModel = new Vocabulary(db);

    // Test the getWordByText method
    console.log('Testing getWordByText method for "beautiful"');
    const beautifulResult = await vocabModel.getWordByText('beautiful');
    console.log('Beautiful result from model method:', beautifulResult);

    console.log('Testing getWordByText method for "word"');
    const wordResult = await vocabModel.getWordByText('word');
    console.log('Word result from model method:', wordResult);

    // Check if 'beautiful' exists with exact case
    const beautifulResultDirect = await db.get("SELECT * FROM vocabulary WHERE word = 'beautiful'");
    console.log('Beautiful result (direct query):', beautifulResultDirect);

    // Check with case-insensitive search
    const beautifulResultCI = await db.get("SELECT * FROM vocabulary WHERE LOWER(word) = 'beautiful'");
    console.log('Beautiful result (case-insensitive):', beautifulResultCI);

    // Check with LIKE
    const beautifulResultLike = await db.get("SELECT * FROM vocabulary WHERE word LIKE 'beautiful'");
    console.log('Beautiful result (LIKE):', beautifulResultLike);

    // Check words starting with 'beautiful'
    const beautifulWords = await db.all("SELECT * FROM vocabulary WHERE word LIKE 'beautiful%'");
    console.log('Words starting with beautiful:', beautifulWords);

    // Check words containing 'beautiful'
    const beautifulWords2 = await db.all("SELECT * FROM vocabulary WHERE word LIKE '%beautiful%'");
    console.log('Words containing beautiful:', beautifulWords2);

    // Check if 'word' exists
    const wordResultDirect = await db.get("SELECT * FROM vocabulary WHERE word = 'word'");
    console.log('Word result (direct query):', wordResultDirect);

    // Check words starting with 'beaut'
    const beautWords = await db.all("SELECT word FROM vocabulary WHERE word LIKE 'beaut%' LIMIT 5");
    console.log('Words starting with beaut:', beautWords);

    // Check total count
    const countResult = await db.get('SELECT COUNT(*) as count FROM vocabulary');
    console.log('Total words in database:', countResult.count);

    // Check some sample words
    const sampleWords = await db.all('SELECT word FROM vocabulary LIMIT 10');
    console.log('Sample words:', sampleWords);
  } catch (error) {
    console.error('Error:', error);
  } finally {
    db.close();
  }
}

checkWords();
